package com.kk.demo.java8.stream.base01;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * 使用stream的filter功能
 * @author liujun
 * @date 2016年8月28日
 * @verion 0.0.1
 */
public class StreamUseFilter {

    public StreamUseFilter() {
    }

    public void filter() {
        // 单用调用filter是不会输出信息
        // 当进行运行后才会有结果输出
        Stream.of("d1", "a2", "b3", "d4").filter(s -> {
            System.out.println("filter1:" + s);
            return true;
        });

        // 仅当有操作时才进行输出
        Stream.of("d1", "a2", "b3", "d4").filter(s -> {
            System.out.println("filter2:" + s);
            return true;
        }).count();
    }

    public void suppile() {
        // 构造对象
        Supplier<Stream<String>> streamSuplie = () -> Stream.of("a1", "b2", "c3", "d4", "e5")
                .filter(s -> s.startsWith("a"));

        // 进行两次的调用
        boolean rsp = streamSuplie.get().anyMatch(s -> true);
        System.out.println("match1 :" + rsp);
        Boolean rsp2 = streamSuplie.get().noneMatch(s -> true);
        System.out.println("match2 :" + rsp2);

        // 如果不使用Supplier对象，则不能使用Stream两次
    }

    /**
     * 进行匹配
     */
    public void anyMatch() {
        boolean match = Stream.of("d1", "a2", "b3", "d4").map(s -> {
            System.out.println("当前s:" + s);
            return s.toUpperCase();
        }).anyMatch(s -> {
            System.out.println("匹配当前信息:" + s);
            return s.startsWith("B");
        });

        System.out.println("当前匹配结果:" + match);
    }

    public static void main(String[] args) {
        StreamUseFilter filter = new StreamUseFilter();
        filter.filter();
        // filter.suppile();
        // filter.anyMatch();
    }

}
